package com.example.fastshare.alljoyn;

import java.io.ObjectOutputStream.PutField;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.List;

import org.alljoyn.bus.BusObject;
import org.alljoyn.bus.Variant;
import org.alljoyn.bus.annotation.Position;
import org.alljoyn.bus.annotation.Signature;

import com.example.fastshare.ProfileInfo;

import android.graphics.Bitmap;

public class Package {

	@Position(0)
	@Signature("s")
	public String m_id;
	@Position(1)
	@Signature("ar")
	public DataPackage[] m_listPackage;
	@Position(2)
	@Signature("s")
	public String m_status;
	@Position(3)
	@Signature("s")
	public String m_type;
	@Position(4)
	@Signature("s")
	public String m_packageName;
	@Position(5)
	@Signature("i")
	public int m_size;

	public synchronized void addPackageImage(Image image) {
		int count = 0;
		for (Bitmap img : image.getM_imageColumn()) {
			String imageData = Utilities.EncodeFromImage(img);
			DataPackage _package = new DataPackage();
			_package.m_id = count + 1;
			_package.m_content = imageData;
			_package.m_length = imageData.length();
			_package.m_isReceived = false;
			m_listPackage[count] = _package;
			count++;
		}
	}

	public synchronized void addPackageString(String data) {
		String message = Utilities.EncodeFromString(data);
		DataPackage _package = new DataPackage();
		_package.m_id = 1;
		_package.m_content = message;
		_package.m_length = message.length();
		_package.m_isReceived = false;
		m_listPackage[0] = _package;
	}

	public synchronized Image getImage() {
		Image result = new Image(0, m_packageName);
		for (DataPackage imageData : m_listPackage) {
			Bitmap image = Utilities.DecodeToImage(imageData.m_content);
			result.AddImage(image);
		}
		return result;
	}

	public synchronized void addPackageInfo(ProfileInfo _info) {
		m_size = 3;
		m_listPackage = new DataPackage[m_size];
		String userName = Utilities.EncodeFromString(MainApp.m_userInfo
				.getM_userName());
		DataPackage userNamePackage = new DataPackage();
		userNamePackage.m_content = userName;
		userNamePackage.m_id = 0;
		userNamePackage.m_isReceived = false;
		userNamePackage.m_length = userName.length();
		m_listPackage[0] = userNamePackage;
		String macAdress = Utilities.EncodeFromString(MainApp.m_userInfo
				.getM_macAddress());
		DataPackage macAdddressPackage = new DataPackage();
		macAdddressPackage.m_content = macAdress;
		macAdddressPackage.m_id = 1;
		macAdddressPackage.m_isReceived = false;
		macAdddressPackage.m_length = macAdress.length();
		m_listPackage[1] = macAdddressPackage;
		String avatar = Utilities.EncodeFromString(MainApp.m_userInfo
				.getM_avatar());
		DataPackage avatarPackage = new DataPackage();
		avatarPackage.m_content = avatar;
		avatarPackage.m_id = 1;
		avatarPackage.m_isReceived = false;
		avatarPackage.m_length = avatar.length();
		m_listPackage[2] = avatarPackage;

	}

	public synchronized ProfileInfo getInfo()
			throws UnsupportedEncodingException {
		String userName = Utilities.DecodeToString(m_listPackage[0].m_content);
		String macAddress = Utilities
				.DecodeToString(m_listPackage[1].m_content);
		String avatar = Utilities.DecodeToString(m_listPackage[2].m_content);
		return new ProfileInfo(userName, macAddress, avatar);
	}

	/*
	 * public synchronized void addPackage(String data) { String temp = data;
	 * int index = 0; int count = (data.length() / PACKAGE_SIZE); int i = 0;
	 * while (i <= count) { if (i < count) { String content =
	 * temp.substring(index, PACKAGE_SIZE * (i + 1)); DataPackage _package = new
	 * DataPackage( m_listPackage.size() + 1, content, content.length());
	 * m_listPackage.add(_package); } else { String content =
	 * temp.substring(index, data.length()); DataPackage _package = new
	 * DataPackage( m_listPackage.size() + 1, content, content.length());
	 * m_listPackage.add(_package); } index += PACKAGE_SIZE; i++; } m_status =
	 * PackageStatus.SENDING;
	 * 
	 * }
	 */

	public synchronized String GetMessage() throws UnsupportedEncodingException {
		return Utilities.DecodeToString(m_listPackage[0].m_content);
	}

}
